/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import javax.swing.JOptionPane;

public class DBConnector {

    // Variables for managing the database connection.
    private boolean connected;
    private Connection conn;
    private Statement stmt;
    private ResultSet rs;
    // Variables for storing database settings.
    private String address;
    private String port;
    private String dbname;
    private String uname;
    private String pass;

    // Constructor.
    public DBConnector() {
        connected = false;
        conn = null;
        stmt = null;
        rs = null;
        address = "";
        port = "";
        dbname = "";
        uname = "";
        pass = "";
    }

    // Connect to the database using the current settings.
    public JResult connect() {
        JResult result;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://" + address + ":" + port + "/" + dbname, uname, pass);
            stmt = conn.createStatement();
            if(stmt == null) JOptionPane.showMessageDialog(null, "Null pointer", "ARGH!!", 0);
            connected = true;
            result = new JResult(true, "Connected to DataBase.");
        } catch (SQLException e) {
            result = new JResult(false, e.getLocalizedMessage());
        } catch (ClassNotFoundException e) {
            result = new JResult(false, e.getLocalizedMessage());
        }
        return result;
    }

    // Disconnect from the database.
    public JResult disconnect() {
        JResult result;
        try {
            stmt.close();
            conn.close();
            connected = false;
            result = new JResult(true, "Disconnected from DataBase.");
        } catch (Exception e) {
            result = new JResult(false, e.getMessage());
        }
        return result;
    }

    // Save database connection info to a file.
    public JResult saveSettings(String FileName) {
        JResult result;
        // If we are connected then disconnect, so that we can test the settings.
        if (connected) {
            disconnect();
        }
        // If the connection is successful the settings are saved.
        JResult con = connect();
        if (con.success) {
            try {
                Properties propertiesOutput = new Properties();
                propertiesOutput.setProperty("Address", address);
                propertiesOutput.setProperty("Port", port);
                propertiesOutput.setProperty("DBName", dbname);
                propertiesOutput.setProperty("UName", uname);
                propertiesOutput.setProperty("Pass", pass);
                propertiesOutput.store(new FileOutputStream(FileName + ".properties"), null);
                result = new JResult(true, "Connected to DataBase.");
            } catch (IOException e) {
                result = new JResult(false, e.getLocalizedMessage());
            }
        } else {
            result = new JResult(false, con.message);
        }
        return result;
    }

    // Load database connection info from a file.
    public JResult loadSettings(String FileName) {
        JResult result;
        try {
            Properties propertiesInput = new Properties();
            propertiesInput.load(new FileInputStream(FileName + ".properties"));
            address = propertiesInput.getProperty("Address");
            port = propertiesInput.getProperty("Port");
            dbname = propertiesInput.getProperty("DBName");
            uname = propertiesInput.getProperty("UName");
            pass = propertiesInput.getProperty("Pass");
            result = new JResult(true);
        } catch (IOException e) {
            result = new JResult(false, e.getMessage());
        }
        return result;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setDBName(String dbname) {
        this.dbname = dbname;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public void setUName(String uname) {
        this.uname = uname;
    }

    public String getAddress() {
        return address;
    }

    public boolean isConnected() {
        return connected;
    }

    public String getDbname() {
        return dbname;
    }

    public String getPass() {
        return pass;
    }

    public String getPort() {
        return port;
    }

    public String getUname() {
        return uname;
    }

    public Statement getStmt() {
        return stmt;
    }
}
